Package cpsc2150.extendedConnectX.models
Class GameBoardMem
java.lang.Object
cpsc2150.extendedConnectX.models.AbsGameBoard
cpsc2150.extendedConnectX.models.GameBoardMem
- All Implemented Interfaces:
IGameBoard
Class that represents an entire game board for the ExtendedConnectX game using a
more memory-efficient map instead of an array.
This class includes methods to place tokens, check token locations, and determine whether or not the game has been won or is over.
- Invariant:
- board.containsKey(' ') = false AND
MIN_ROWS
<=numRows<MAX_ROWS AND MIN_COLUMNS<=numColumns<MAX_COLUMNS MIN_NUM_TO_WIN<=numToWin<=MAX_NUM_TO_WIN
-
Field Summary
Fields inherited from interface cpsc2150.extendedConnectX.models.IGameBoard
MAX_COLUMNS, MAX_NUM_TO_WIN, MAX_ROWS, MIN_COLUMNS, MIN_NUM_TO_WIN, MIN_ROWS -
Constructor Summary
ConstructorsConstructorDescriptionGameBoardMem(int rows, int columns, int win) Constructor for GameBoardMem that takes parameters for the number of rows, the number of columns, and the number needed to win, and initializes a HashMap for the game board. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of columns in the GameBoard.intReturns the number of rows in the GameBoard.intReturns the number of tokens in a row needed to win the game.booleanisPlayerAtPos(BoardPosition pos, char player) This function returns true if the player is at pos; otherwise, it returns false.voidplaceToken(char p, int c) Function that places the character p in column c.charwhatsAtPos(BoardPosition pos) Function that returns what is in the GameBoard at position pos.Methods inherited from class cpsc2150.extendedConnectX.models.AbsGameBoard
toStringMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface cpsc2150.extendedConnectX.models.IGameBoard
checkDiagWin, checkForWin, checkHorizWin, checkIfFree, checkTie, checkVertWin
-
Constructor Details
-
GameBoardMem
public GameBoardMem(int rows, int columns, int win) Constructor for GameBoardMem that takes parameters for the number of rows, the number of columns, and the number needed to win, and initializes a HashMap for the game board.- Parameters:
rows- Number of rowscolumns- Number of columnswin- Number needed to win- Pre:
- MIN_ROWS
<=rows<MAX_ROWS AND MIN_COLUMNS<=columns<MAX_COLUMNS AND MIN_NUM_TO_WIN<=win<=MAX_NUM_TO_WIN AND win<=rows AND win<=columns - Post:
- board = [ empty HashMap with key type Character and value type BoardPosition ] AND numRows = rows AND numColumns = columns AND numToWin = win
-
-
Method Details
-
placeToken
public void placeToken(char p, int c) Description copied from interface:IGameBoardFunction that places the character p in column c. The token will be placed in the lowest available row in column c.- Specified by:
placeTokenin interfaceIGameBoard- Parameters:
p- Character to be placed (representing player token)c- Column to place token in
-
whatsAtPos
Description copied from interface:IGameBoardFunction that returns what is in the GameBoard at position pos. If no marker is there, it returns a blank space char.- Specified by:
whatsAtPosin interfaceIGameBoard- Parameters:
pos- BoardPosition pair of row and column- Returns:
- Character representing player token or space
-
isPlayerAtPos
This function returns true if the player is at pos; otherwise, it returns false.- Specified by:
isPlayerAtPosin interfaceIGameBoard- Parameters:
pos- BoardPosition pair of row and columnplayer- Player to check with- Returns:
- Whether a player's token is at a position
- Pre:
- (0
>=pos.row<numRows AND 0>=pos.column<numColumns) AND player != ' ' - Post:
- isPlayerAtPos = true iff (board.containsKey(player) = true AND board.get(player).contains(pos) = true) AND board = #board isPlayerAtPos = false iff (board.containsKey(player) = false) AND board = #board isPlayerAtPos = false iff (board.containsKey(player) = true AND board.get(player).contains(pos) = false) AND board = #board
-
getNumRows
public int getNumRows()Description copied from interface:IGameBoardReturns the number of rows in the GameBoard.- Specified by:
getNumRowsin interfaceIGameBoard- Returns:
- The number of rows
-
getNumColumns
public int getNumColumns()Description copied from interface:IGameBoardReturns the number of columns in the GameBoard.- Specified by:
getNumColumnsin interfaceIGameBoard- Returns:
- The number of columns
-
getNumToWin
public int getNumToWin()Description copied from interface:IGameBoardReturns the number of tokens in a row needed to win the game.- Specified by:
getNumToWinin interfaceIGameBoard- Returns:
- The number of tokens
-